$(function () {
    var audit_datagrid = $("#audit_datagrid");
    var audit_dialog = $("#audit_dialog");
    var audit_form = $("#audit_form");
    var detail_dialog = $("#detail_dialog");
    var detail_form = $("#detail_form");
    var detail_datagrid = $("#detail_datagrid");

    audit_datagrid.datagrid({
        toolbar: '#audit_toolbar',
        url: '/accdentdetail/auditquery.do',
        fit: true,
        fitColumns: true,
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '保单号', field: 'guardNum',align:'center', width: '100'},
            {title: '车牌号', field: 'carnumber',align:'center', width: '100'},
            {title: '报案人', field: 'telname',align:'center', width: '100'},
            {title: '报案人电话', field: 'tel',align:'center', width: '100'},
            {title: '发生地点', field: 'happenplace',align:'center', width: '100'},
            {title: '受理时间', field: 'acceptDate',align:'center', width: '100'},
            {title: '事故描述', field: 'accdentdetail',align:'center', width: '100'},
            {title: '责任认定', field: 'accdentduty',align:'center', width: '100'},
            {title: '赔损单', field: 'compensation_id',align:'center', width: '100',formatter:function(value, row, index){
        return '<a href="javascript:details();" name="details">赔偿明细</a>'}},
            {
                title: '状态', field: 'auditState',align:'center', width: '100', formatter: function (value, row, index) {
                    if(value == 0){
                        return "<font color='red'>待审核</font>";
                    } else {
                        return "<font color='green'>已审核</font>";
                    }
                }
            },
            {title: '备注', field: 'note',align:'center', width: '100'},
        ]],
        onLoadSuccess:function (data) {
            $("a[name='details']").linkbutton({text:'赔偿明细',plain:true});
        }
    })


   /* //明细
    detail_dialog.dialog({
        width: 350,
        height: 380,
        buttons: '#audit_buttons'
    })*/

   /* //明细
    detail_dialog.dialog({
        title:"赔偿清单",
        width: 350,
        height: 380,
        //url:"/claim/claimlist.do",
        buttons: '#detail_buttons',
        closed: true,
        onClose: function () {
            detail_form.form("clear")
        }
    })*/

    audit_dialog.dialog({
        width: 350,
        height: 380,
        buttons: '#audit_buttons',
        closed: true,
        onClose: function () {
            audit_form.form("clear")
        }
    })

    var methodObj = {
        //明细
        tail:function () {
            var data = audit_datagrid.datagrid('getSelected');
            if(!data){
                $.messager.alert("温馨提示","请至少选中一条数据");
                return;
            }
            detail_dialog.dialog("open");
            if(data.claim.cardnumber){
                data["claim.cardnumber"]=data.claim.cardnumber;
            }
            if(data.claim.client){
                data["claim.client"]=data.claim.client;
            }
            if(data.claim.amount){
                data["claim.amount"]=data.claim.amount;
            }
            if(data.claim.damagcondition){
                data["claim.damagcondition"]=data.claim.damagcondition;
            }
            detail_form.form('load',data);
        },
        //取消按钮
        cancel_tail: function () {
            detail_dialog.dialog("close");
        },
        //添加保存按钮
        save_tail: function () {
            var data = audit_datagrid.datagrid('getSelected');
            detail_form.form('submit', {
                url: '/claim/updateDetail.do',
                onSubmit: function(data){
                    data.id = d.id;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            detail_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        },

        //审核
        audit:function () {
          var data = audit_datagrid.datagrid('getSelected');
          if(!data){
              $.messager.alert("温馨提示","请至少选中一条数据!");
              return;
          } else if(data.auditState==1){
              $.messager.alert("温馨提示","请选择待审核数据!");
              return;
          } else {
              $.messager.confirm("温馨提示","确定审核该案件吗?",function () {
                  $.get("/accdentdetail/changeState.do",{"ids":data.id,"id":data.guard.id,
                                                        "client.id":data.guard.client.id,"product.id":data.guard.product.id,
                                                        "product.promise.id":data.guard.product.promise.id,
                                                        "amount":data.claim.amount},function (r) {
                      if (r.success) {
                          $.messager.alert("温馨提示", "审核成功", "info", function () {
                              audit_datagrid.datagrid("reload");
                              return;
                          })
                      } else {
                          $.messager.alert('温馨提示', data.msg);
                      }
                      audit_datagrid.datagrid("reload");
                      return;
                  })
              })
          }

        },

        //编辑按钮
        edit: function () {
            var data = audit_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            audit_form.form('load', data);
            audit_dialog.dialog("open");
            audit_dialog.dialog('setTitle', '编辑事故细节');
        },
        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");

            audit_datagrid.datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });
        },

        //刷新按钮
        reload: function () {
            audit_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            audit_dialog.dialog("close");
        },
        //添加保存按钮
        save: function () {
            audit_form.form('submit', {
                url: '/accdentdetail/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            audit_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }

            })
            audit_dialog.datagrid("reload");
            return;
        }
    }


    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

})
function details() {
    $("#detail_dialog").dialog({
        title:"赔偿明细",
        width: 320,
        height: 320,
        onBeforeOpen:function () {
            var data = $("#audit_datagrid").datagrid('getSelected');
            if(data.claim.cardnumber){
                data["claim.cardnumber"]=data.claim.cardnumber;
                $("#detail_form").form('load',data);
            }
            if(data.claim.client){
                data["claim.client"]=data.claim.client;
                $("#detail_form").form('load',data);
            }
            if(data.claim.amount){
                data["claim.amount"]=data.claim.amount;
                $("#detail_form").form('load',data);
            }
            if(data.claim.damagcondition){
                data["claim.damagcondition"]=data.claim.damagcondition;
                $("#detail_form").form('load',data);
            }
        },
        onBeforeCollapse:function () {
            $("#detail_dialog").dialog("clear");
        }
    });
}



